<template>
  <div class="search-result">
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  :error.sync="error"
  error-text="加载失败，点击下试试"
  @load="onLoad"
>
<!-- <van-cell v-for="item in list" :key="item" :title="item" />
 -->
   <van-cell
        v-for="(article, index) in list"
        :key="index"
        :title="article.title"
      />
</van-list>
  </div>
</template>

<script>
import {getSearchResult} from '@/api/search'
export default {
    name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      size: 20,
      error: false
    }
  },
props: {
   searchText: {
      type: String,
      required: true
    }
},
  components: {},

  computed: {},

  mounted(){},

 // watch(){},

  methods: {
   async onLoad (){
    try {
      // debugger
      const { data } = await getSearchResult({
          page: this.page, // 页码
          per_page: this.size, // 每页大小
          q: this.searchText // 查询关键词
     }) 
     const { results } = data.data
     this.list.push(...results)
     this.loading = false
     if(results.length){
      this.page++

     }
     else{
      this.finished = true
     }
    } catch (err) {
      console.log(err);
      this.error = true
      this.loading = false
      this.$toast('搜索不到结果');
    }
    

    }
  }
}

</script>
<style scoped>
.search-result{
   height: 101vh;
  overflow-y: auto;
}
</style>